using System.Collections;
using System.Collections.Generic;
using QFramework;
using UnityEngine;

/// <summary>
/// 放所有的全局变量
/// </summary>
namespace ProjectSurvivor
{
    public class Global : Architecture<Global>
    {
        #region Model
        public static BindableProperty<int> MaxHp = new BindableProperty<int>(3);
        /// <summary>
        /// 玩家血量
        /// </summary>
        public static BindableProperty<int> HP = new BindableProperty<int>(3);
        /// <summary>
        /// 经验值
        /// </summary>
        public static BindableProperty<int> Exp = new BindableProperty<int>(0);

        public static BindableProperty<int> coin = new BindableProperty<int>(0);

        /// <summary>
        /// 等级
        /// </summary>
        public static BindableProperty<int> Level = new BindableProperty<int>(1);

        /// <summary>
        /// 攻击
        /// </summary>
        public static BindableProperty<float> SimpleAbilityDamage = new BindableProperty<float>(5);
        public static BindableProperty<float> SimpleAbilityDuration = new BindableProperty<float>(1.5f);

        /// <summary>
        /// 时间
        /// </summary>
        public static BindableProperty<float> CurrentSeconds = new BindableProperty<float>(0);


        public static BindableProperty<float> CoinPercent = new BindableProperty<float>(0.1f);
        public static BindableProperty<float> ExpPercent = new BindableProperty<float>(0.4f);

        /// <summary>
        /// SimpleSword数量
        /// </summary>
        public static BindableProperty<int> SimpleSwordCount = new(Config.InitSimpleSwordCount);

        /// <summary>
        /// SimpleSword范围
        /// </summary>
        public static BindableProperty<bool> SimpleSwordUnlocked = new(false);
        public static BindableProperty<float> SimpleSwordRange = new(Config.InitSimpleSwordRange);


        public static BindableProperty<bool> SimpleKnifeUnlocked = new(false);
        public static BindableProperty<float> SimpleKnifeDamage = new BindableProperty<float>(Config.InitSimpleKnifeDamage);
        public static BindableProperty<float> SimpleKnifeDuration = new BindableProperty<float>(Config.InitSimpleKnifeDuration);
        public static BindableProperty<int> SimpleKnifeCount = new BindableProperty<int>(Config.InitSimpleKnifeCount);

        public static BindableProperty<int> SimpleKnifeAttackCount = new(1);//穿透数量


        public static BindableProperty<bool> RotateSwordUnlocked = new(false);
        public static BindableProperty<float> RotateSwordDamage = new(Config.InitRotateSwordDamage);
        public static BindableProperty<int> RotateSwordCount = new(Config.InitRotateSwordCount);
        public static BindableProperty<float> RotateSwordSpeed = new(Config.InitRotateSwordSpeed);
        public static BindableProperty<float> RotateSwordRange = new(Config.InitRotateSwordRange);

        public static BindableProperty<bool> BasketballUnlocked = new(false);
        public static BindableProperty<float> BasketBallDamage = new(Config.InitBasketBallDamage);
        public static BindableProperty<float> BasketBallSpeed = new(Config.InitBasketBallSpeed);
        public static BindableProperty<int> BasketBallCount = new(Config.InitBasketBallCount);



public static BindableProperty<bool> BombUnlocked = new(false);
        public static BindableProperty<float> BombDamage = new(Config.InitBombDamage);
        public static BindableProperty<float> BombPercent = new(Config.InitBombPercent);

        // public static BindableProperty<float> BombDamage = new(Config.InitBombDamage);
        // public static BindableProperty<float> BombPercent = new(Config.InitBombPercent);

        #endregion

        [RuntimeInitializeOnLoadMethod]
        public static void AtoInit()
        {
            ResKit.Init();
            UIKit.Root.SetResolution(1920, 1080, 1);
            Global.coin.Value = PlayerPrefs.GetInt("coin", 0);

            Global.MaxHp.Value = PlayerPrefs.GetInt(nameof(MaxHp), 3);
            HP.Value = MaxHp.Value;
            Global.ExpPercent.Value = PlayerPrefs.GetFloat(nameof(ExpPercent), 0.4f);
            Global.CoinPercent.Value = PlayerPrefs.GetFloat(nameof(CoinPercent), 0.1f);


            Global.coin.Register(coin =>
            {
                PlayerPrefs.SetInt(nameof(coin), coin);

            });

            Global.ExpPercent.Register(exppercent =>
            {
                PlayerPrefs.SetFloat(nameof(ExpPercent), exppercent);

            });

            Global.CoinPercent.Register(coinPercent =>
            {
                PlayerPrefs.SetFloat(nameof(CoinPercent), coinPercent);

            });

            Global.MaxHp.Register(maxHP =>
            {
                PlayerPrefs.SetInt(nameof(MaxHp), maxHP);
            });

            var _ = Interface;
        }
        /// <summary>
        /// 重置
        /// </summary>
        public static void ResetData()
        {
            HP.Value = MaxHp.Value;
            Exp.Value = 0;
            Level.Value = 1;
            SimpleAbilityDamage.Value = 1;
            SimpleAbilityDuration.Value = 1.5f;
            CurrentSeconds.Value = 0;
            SimpleAbilityDamage.Value = Config.InitSimpleSwordDamage;
            SimpleAbilityDuration.Value = Config.InitSimpleSwordDuration;
            SimpleSwordRange.Value = Config.InitSimpleSwordRange;
            SimpleSwordCount.Value = Config.InitSimpleSwordCount;

            SimpleKnifeDamage.Value = Config.InitSimpleKnifeDamage;
            SimpleKnifeDuration.Value = Config.InitSimpleKnifeDuration;
            SimpleKnifeCount.Value = Config.InitSimpleKnifeCount;


            RotateSwordDamage.Value = Config.InitRotateSwordDamage;
            RotateSwordCount.Value = Config.InitRotateSwordCount;
            RotateSwordSpeed.Value = Config.InitRotateSwordSpeed;
            RotateSwordRange.Value = Config.InitRotateSwordRange;


            BasketballUnlocked.Value = false;
            BasketBallDamage.Value = Config.InitBasketBallDamage;
            BasketBallSpeed.Value = Config.InitBasketBallSpeed;
            BasketBallCount.Value = Config.InitBasketBallCount;


            BombDamage.Value = Config.InitBombDamage;
            BombPercent.Value = Config.InitBombPercent;
            EnemyGenerator.EnemyCount.Value = 0;
            Interface.GetSystem<ExpUpgradeSystem>().ReseData();
        }

        public static int ExpToNextLevel()
        {
            return Level.Value * 5;
        }

        public static void GeneratePowerUp(GameObject gameObject)
        {
            var percent = Random.Range(0, 1f);

            if (percent < ExpPercent.Value)
            {
                PowerUpManager.Default.Exp.Instantiate()
                .Position(gameObject.Position())
                .Show();
                return;
            }

            percent = Random.Range(0, 1);
            if (percent < CoinPercent.Value)
            {
                PowerUpManager.Default.Coin.Instantiate()
                .Position(gameObject.Position())
                .Show();
                return;
            }

            percent = Random.Range(0, 1f);

            if (percent < 0.1f)
            {
                PowerUpManager.Default.Hp.Instantiate()
                .Position(gameObject.Position())
                .Show();
                return;
            }

            percent = Random.Range(0, 1f);

            if (percent < 0.1f)
            {
                PowerUpManager.Default.Bong.Instantiate()
                .Position(gameObject.Position())
                .Show();
                return;
            }

            percent = Random.Range(0, 1f);

            if (percent < 0.1f)
            {
                PowerUpManager.Default.GetAllExp.Instantiate()
                .Position(gameObject.Position())
                .Show();
                return;
            }

        }


        protected override void Init()
        {
            this.RegisterSystem(new SaveSystem());
            this.RegisterSystem(new ICoinUpgradeSystem());
            this.RegisterSystem(new ExpUpgradeSystem());
        }
    }
}
